package com.ssl.sslliving.commodity.service.impl;

import com.ssl.sslliving.commodity.dao.SpuInfoDao;
import com.ssl.sslliving.commodity.entity.SpuInfoEntity;
import com.ssl.sslliving.commodity.vo.SearchResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssl.common.utils.PageUtils;
import com.ssl.common.utils.Query;

import com.ssl.sslliving.commodity.dao.SkuInfoDao;
import com.ssl.sslliving.commodity.entity.SkuInfoEntity;
import com.ssl.sslliving.commodity.service.SkuInfoService;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.and(wrapper -> {
                wrapper.eq("sku_id", key).or().like("sku_name", key);
            });
        }
        String catalogId = (String) params.get("catalogId");
        if (StringUtils.isNotBlank(catalogId) && !"0".equalsIgnoreCase(catalogId)) {
            queryWrapper.eq("catalog_id", catalogId);
        }
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId) && !"0".equalsIgnoreCase(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        String min = (String) params.get("min");
        if (StringUtils.isNotBlank(min) && !"0".equalsIgnoreCase(min)) {
            queryWrapper.ge("price", min);
        }
        String max = (String) params.get("max");
        if (StringUtils.isNotBlank(max) && !"0".equalsIgnoreCase(max)) {
            queryWrapper.le("price", max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }


    @Resource
    private SpuInfoDao spuInfoDao;

    @Override
    public SearchResult querySearchByCondition(Map<String, Object> params) {

        List<SpuInfoEntity> spuInfoEntities =
                spuInfoDao.selectList(new QueryWrapper<SpuInfoEntity>().eq("publish_status", 1));
        List<Long> spuInfoEntitiesIds = spuInfoEntities.stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toList());


        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        if (spuInfoEntitiesIds != null && spuInfoEntitiesIds.size() > 0) {
            queryWrapper.in("spu_id", spuInfoEntitiesIds);
        } else {
            // 如果没有上架的商品，后面再进行处理
            SearchResult searchResult = new SearchResult();
            searchResult.setPageNum(1);
            searchResult.setTotal(0L);
            searchResult.setTotalPages(0);
            searchResult.setCommodity(new ArrayList<>());
            searchResult.setCommodity(new ArrayList<>());
            searchResult.setKeyword(params.get("keyword") == null ? "" : params.get("keyword").toString());
            return searchResult;
        }

        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.and(wrapper -> {
                wrapper.eq("sku_id", keyword).or().like("sku_name", keyword);
            });
        }
        String catalog3Id = (String) params.get("catalog3Id");
        if (StringUtils.isNotBlank(catalog3Id) && !"0".equalsIgnoreCase(catalog3Id)) {
            queryWrapper.eq("catalog_id", catalog3Id);
        }
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId) && !"0".equalsIgnoreCase(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        params.put("limit", "2");
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        SearchResult searchResult = new SearchResult();
        searchResult.setCommodity((List<SkuInfoEntity>) pageUtils.getList());
        searchResult.setPageNum(pageUtils.getCurrPage());
        searchResult.setTotal((long) pageUtils.getTotalCount());
        searchResult.setTotalPages(pageUtils.getTotalPage());
        ArrayList<Integer> pageNavs = new ArrayList<>();
        for (int i = 1; i <= pageUtils.getTotalPage(); i++) {
            pageNavs.add(i);
        }
        searchResult.setPageNavs(pageNavs);
        searchResult.setKeyword(params.get("keyword") == null ? "" : params.get("keyword").toString());
        return searchResult;
    }

}